//
//  AlbumSelectImageController.m
//  ArtDog
//
//  Created by dllo on 16/9/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AlbumSelectImageController.h"
#import <Photos/Photos.h>
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "ImageViewCollectionCell.h"
#import "AlbumToolbar.h"
#import "ReminderView.h"
#import "AlbumNavigationController.h"
#import "BaseTableView.h"

@interface AlbumSelectImageController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) BaseCollectionView *collectionView;

@property (nonatomic, strong) UILabel *countLabel;

@property (nonatomic, strong) PHImageManager *imageManager;

@property (nonatomic, strong) NSMutableArray *isSelects;

@property (nonatomic, strong) AlbumToolbar *toolbar;

@end

@implementation AlbumSelectImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
    [self setupOtherViews];
}

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    _imageManager = [[PHImageManager alloc]init];
    _isSelects = [NSMutableArray array];
    for (NSInteger i = 0; i < _dataArray.count; i++) {
        [_isSelects addObject:@"0"];
    }
    [_collectionView reloadData];
}

- (void)setupOtherViews
{
    _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _collectionView.contentSize.height, SCREEN_WIDTH - 30, 200)];
    [_collectionView addSubview:_countLabel];
    _countLabel.font = [UIFont systemFontOfSize:17];
    _countLabel.text = [NSString stringWithFormat:@"一共有%ld张图片", _dataArray.count];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    
    _toolbar = [[AlbumToolbar alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_collectionView.frame), SCREEN_WIDTH, 49)];
    [self.view addSubview:_toolbar];
    [_toolbar.finishBtn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)finishAction
{
    AlbumNavigationController *naviVC = (AlbumNavigationController *)self.navigationController;
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        for (PHAsset *asset in _toolbar.dataArray) {
            [naviVC.writeTopicVC.imageArray addObject:asset];
        }
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [naviVC.writeTopicVC.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 35) / 4, (SCREEN_WIDTH - 35) / 4);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 0;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _collectionView.contentInset = UIEdgeInsetsMake(10, 10, 100, 10);
    
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageViewCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageViewCollectionCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCollectionCell" forIndexPath:indexPath];
    
    PHAsset *asset = _dataArray[indexPath.item];
    [_imageManager requestImageForAsset:asset targetSize:cell.frame.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        
        if (result) {
            cell.imageView.image = result;
        }
    }];
    if ([_isSelects[indexPath.item] isEqualToString:@"0"]) {
        cell.selectView.hidden = YES;
        if (_toolbar.dataArray.count != 0) {
            [_toolbar.dataArray removeObject:_dataArray[indexPath.item]];
        }
    } else {
        cell.selectView.hidden = NO;
        [_toolbar.dataArray addObject:_dataArray[indexPath.item]];
    }
    if (_countLabel.frame.origin.y == 0) {
        _countLabel.frame = CGRectMake(0, _collectionView.contentSize.height + 40, SCREEN_WIDTH - 20, 20);
        collectionView.contentOffset = CGPointMake(0, collectionView.contentSize.height - SCREEN_HEIGHT + 64 + 49 + 100);
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageViewCollectionCell *cell = (ImageViewCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.selectView.hidden) {
        AlbumNavigationController *naviVC = (AlbumNavigationController *)self.navigationController;
        if (_toolbar.dataArray.count == naviVC.selectCount) {
            [ReminderView reminderWithSuperView:_toolbar andTitle:[NSString stringWithFormat:@"最多选择%ld张图片！", naviVC.selectCount] andPosition:HLAlertViewPositionUp];
            return;
        }
        [_isSelects replaceObjectAtIndex:indexPath.item withObject:@"1"];
    } else {
        [_isSelects replaceObjectAtIndex:indexPath.item withObject:@"0"];
    }
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
